import { _decorator, Node } from 'cc';
import { App } from '../../App';
import UtilTool from '../../tools/UtilTool';
import AbstractComponent from '../../base/AbstractComponent';
import { EditBox } from 'cc';
import { showErrorTip, showTip } from '../../tools/PopuUp';
import CommConst from '../../const/CommConst';
import wsTool from '../../net/ws';
import { PD } from '../../player/PlayerData';
const { ccclass, property } = _decorator;

// 设置昵称界面
@ccclass('SetNickPanel')
export class SetNickPanel extends AbstractComponent {
    @property(Node)
    bg: Node = null

    @property(EditBox)
    nickEditBox: EditBox = null

    nickLabel = ""
    onLoad() {
        UtilTool.setScreen(this.node)
        UtilTool.nodeActive(this.bg, 5)
    }

    start() {
        // 输入框
        this.nickEditBox.inputMode = EditBox.InputMode.SINGLE_LINE;
        this.nickEditBox.node.on('text-changed',(e)=>{
            this.nickLabel = this.nickEditBox.string
        })

        this.nickEditBox.node.on('editing-did-ended',(e)=>{
            this.nickLabel = this.nickEditBox.string
        })
    }

    setData(obj, data) {
      
    }

    /**
     * 确定购买
     */
    clickSure() {
        App.Audio.playOneShot("click")

        if (this.nickLabel=="" || this.nickLabel==null){
            showTip(App.language.getLanguageLab(184))
            return
        }

        wsTool.send({msgCode:CommConst.Req_MODIFY_NICK, "nickname":this.nickLabel, "headId":null})
        .then((data) => {
            if(data.result == 0) {
                showTip(App.language.getLanguageLab(185))
                PD.setNick(this.nickLabel)
                this.node.destroy()

            } else {
                showErrorTip(data.result)
            }
        })
    }

    /**
     * 取消购买
     */
    clickCancel() {
        App.Audio.playOneShot("click")
        this.node.destroy()
    }

}

